package model

import (
	"github.com/go-git/go-git/v5"
)

// 存储git仓库的实体类
type GitModel struct {
	ID     uint   `gorm:"primarykey" json:"id"`
	Name   string `json:"name" gorm:"unique"`
	Url    string `json:"url"`
	Path   string `json:"path"`
	AuthID uint   `json:"authid"`
	Status string `json:"status"`
}

type GitAuth struct {
	ID       uint   `gorm:"primarykey" json:"id"`
	Username string `json:"username"`
	Password string `json:"password"`
}
type GitMonitorInter struct {
	UUID   string `gorm:"primarykey" json:"id"`
	Gitid  uint   `json:"gitid"`
	Status string `json:"status"`
	Name   string `json:"name"`
}

func (gm GitModel) OpenRepo() (re *git.Repository, err error) {
	re, err = git.PlainOpen(gm.Path)
	return
}

// Get a remote from the repo
func (gm GitModel) GetRemote() (git.Remote, error) {
	re, err := gm.OpenRepo()
	if err != nil {
		return git.Remote{}, err
	}
	remotes, err := re.Remotes()
	if len(remotes) < 1 {
		return git.Remote{}, &NoRemoteErr{}
	}
	return *remotes[0], err
}

type NoRemoteErr struct{}

func (e NoRemoteErr) Error() string {
	return "no remote repo in repo"
}
